package tommysmapeditor.helper.exporter;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;

import tommysmapeditor.helper.ExportHelper;
import tommysmapeditor.input.MapEditorInput;

public class JakoFormatExporter implements ExporterFactory {

	public void exportToCpp(MapEditorInput mei, String dirPath,String mapName) {

		
		try {
			
			String mapNameAndPath = dirPath+"/"+mapName;
			ExportHelper.eraseFileIfPreExisting(mapNameAndPath);
			
			writeExportFile(mei,dirPath+"/"+mapName);

		//TODO to be replaced by direct memory save
//		ThomasExportHelper.copyBmpTilesToExportDir(mei.EDITOR_WORK_TILES_PATH, mei.tileFilePaths, dirPath);
			
		ExportHelper.directSaveOfMemoryTiles(mei,dirPath);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}




	void writeExportFile(MapEditorInput mei, String fileAndPath) throws IOException{
		FileWriter saveFile = null;
		try {
			saveFile = new FileWriter(fileAndPath);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		BufferedWriter bw = new BufferedWriter(saveFile);
		bw.write(String.valueOf(mei._tileSize));
		bw.newLine();
		bw.write(String.valueOf(mei.getTileWidth()));
		bw.newLine();
		bw.write(String.valueOf(mei.getTileHeight()));
		bw.newLine();
		bw.write(String.valueOf(mei._tileNumber));
		bw.newLine();
	
		//line itc
		for( int j=0;j<mei.getTileHeight(); j++ ){
			String mapLine="";
			//col itc
			for(int i=0; i<mei.getTileWidth(); i++){
				mapLine+= String.valueOf(mei.tileGrid[i][j]);
				
				if(i!=(mei.getTileWidth()-1)){
					mapLine+=",";
				}
			}
			bw.write(mapLine);
			bw.newLine();
		}
		
		for(int z=0;z<mei.tileFilePaths.length;z++ ){
			bw.write(mei.optionalTileNamePrefix+"/"+mei.tileFilePaths[z]);
			bw.newLine();
		}
		bw.flush();
		bw.close();
		
	}
	
	
	
}
